<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAdminTables extends Migration
{
    public function getConnection()
    {
        return $this->config('database.connection') ?: config('database.default');
    }

    public function config($key)
    {
        return config('admin.'.$key);
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->config('database.users_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            // 基本信息
            $table->string('username', 120)->unique();
            $table->string('password', 80);
            $table->string('name');
            $table->string('avatar')->nullable();
            $table->string('remember_token', 100)->nullable();

            $table->string('id_card')->after('username')->nullable()->comment('身份证号（唯一，必填）');
            $table->timestamp('id_verified_at')->nullable()->after('id_card')->comment('身份验证时间');
            $table->string('phone')->after('id_verified_at')->comment('手机号（必填）');
            $table->timestamp('phone_verified_at')->nullable()->after('phone')->comment('手机验证时间');
            $table->string('email')->after('phone_verified_at')->comment('电子邮箱（唯一，必填）');
            $table->timestamp('email_verified_at')->nullable()->after('email')->comment('邮件验证时间');
            $table->string('dept_id')->nullable(); // 添加部门字段
            // 用户类型
            $table->tinyInteger('user_type')->after('password')->comment('用户类型（1: 校友，2: 档案员）');

            // 本科信息
            $table->string('undergrad_college')->nullable()->after('user_type')->comment('本科阶段学院');
            $table->string('undergrad_major')->nullable()->after('undergrad_college')->comment('本科专业');
            $table->string('undergrad_origin')->nullable()->after('undergrad_major')->comment('本科生源地');
            $table->date('undergrad_admission_date')->nullable()->after('undergrad_origin')->comment('本科入学日期');
            $table->date('undergrad_graduation_date')->nullable()->after('undergrad_admission_date')->comment('本科毕业日期');

            // 硕士信息
            $table->string('master_college')->nullable()->after('undergrad_graduation_date')->comment('硕士学院');
            $table->string('master_major')->nullable()->after('master_college')->comment('硕士专业');
            $table->date('master_admission_date')->nullable()->after('master_major')->comment('硕士入学日期');
            $table->date('master_graduation_date')->nullable()->after('master_admission_date')->comment('硕士毕业日期');

            // 博士信息
            $table->string('phd_college')->nullable()->after('master_graduation_date')->comment('博士学院');
            $table->string('phd_major')->nullable()->after('phd_college')->comment('博士专业');
            $table->date('phd_admission_date')->nullable()->after('phd_major')->comment('博士入学日期');
            $table->date('phd_graduation_date')->nullable()->after('phd_admission_date')->comment('博士毕业日期');

            // 教育类型
            $table->string('education_type')->nullable()->after('phd_graduation_date')->comment('教育类型（1:全日制 / 2:在职 / 3:网络教育）');

            $table->timestamps();
        });

        Schema::create($this->config('database.roles_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 50);
            $table->string('slug', 50)->unique();
            $table->timestamps();
        });

        Schema::create($this->config('database.permissions_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 50);
            $table->string('slug', 50)->unique();
            $table->string('http_method')->nullable();
            $table->text('http_path')->nullable();
            $table->integer('order')->default(0);
            $table->bigInteger('parent_id')->default(0);
            $table->timestamps();
        });

        Schema::create($this->config('database.menu_table'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('parent_id')->default(0);
            $table->integer('order')->default(0);
            $table->string('title', 50);
            $table->string('icon', 50)->nullable();
            $table->string('uri', 50)->nullable();

            $table->timestamps();
        });

        Schema::create($this->config('database.role_users_table'), function (Blueprint $table) {
            $table->bigInteger('role_id');
            $table->bigInteger('user_id');
            $table->unique(['role_id', 'user_id']);
            $table->timestamps();
        });

        Schema::create($this->config('database.role_permissions_table'), function (Blueprint $table) {
            $table->bigInteger('role_id');
            $table->bigInteger('permission_id');
            $table->unique(['role_id', 'permission_id']);
            $table->timestamps();
        });

        Schema::create($this->config('database.role_menu_table'), function (Blueprint $table) {
            $table->bigInteger('role_id');
            $table->bigInteger('menu_id');
            $table->unique(['role_id', 'menu_id']);
            $table->timestamps();
        });

        Schema::create($this->config('database.permission_menu_table'), function (Blueprint $table) {
            $table->bigInteger('permission_id');
            $table->bigInteger('menu_id');
            $table->unique(['permission_id', 'menu_id']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->config('database.users_table'));
        Schema::dropIfExists($this->config('database.roles_table'));
        Schema::dropIfExists($this->config('database.permissions_table'));
        Schema::dropIfExists($this->config('database.menu_table'));
        Schema::dropIfExists($this->config('database.role_users_table'));
        Schema::dropIfExists($this->config('database.role_permissions_table'));
        Schema::dropIfExists($this->config('database.role_menu_table'));
        Schema::dropIfExists($this->config('database.permission_menu_table'));
    }
}
